﻿#region Disclaimer/Info
///////////////////////////////////////////////////////////////////////////////////////////////////
// MetaCollection
// 
// MetaCollection is a system to provide a standard structure, permissions and metadata service
// for content organization.
//
// This project is hosted at Google Code at http://code.google.com/p/metacollection/
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.IO;

namespace MetaCollection.Framework.Model
{
	/// <summary>
	/// A metadata property associate with an ItemInstance.
	/// 
	/// These values are used as the extensible part of the Item Model.  An IProvider (ResourceProvider)
	/// will apply validation to these properties to create new Model objects off of an Item with these
	/// properties.  An example of this would be a News Article Item Model where these properties may store
	/// bylines or sub headlines.  The validation of what is in and if these properties are required falls
	/// to the IProvider that handles the requests.  The configuration of this IProvider is associated with
	/// the item through the IProviderType reference in the IItemInstance
	/// </summary>
	public interface IItemProperty
	{
		/// <summary>
		/// Property Key
		/// </summary>
		string KeyName { get; set; }
		/// <summary>
		/// Value associated with <see cref="KeyName"/>
		/// </summary>
		string KeyValue { get; set; }
	}
	/// <summary>
	/// A structure representing an ItemProperty
	/// </summary>
	[Serializable()]
	public struct ItemProperty : IItemProperty
	{
		/// <summary>
		/// Property Key
		/// </summary>
		public string KeyName { get; set; }
		/// <summary>
		/// Value associated with <see cref="KeyName"/>
		/// </summary>
		public string KeyValue { get; set; }
	}

	/// <summary>
	/// Property Data Serialization.
	/// </summary>
	[ProtoBuf.ProtoContract]
	public class ItemPropertySurrogate : IItemProperty
	{
		[ProtoBuf.ProtoMember(1)]
		public string KeyName { get; set; }
		[ProtoBuf.ProtoMember(2)]
		public string KeyValue { get; set; }
	}

	public static partial class Extensions
	{
		/// <summary>
		/// Converts an enumerable set of IItemProperty to a simple structure implementation of the interface
		/// </summary>
		/// <param name="properties"></param>
		/// <returns>An enumerable collection of Item Property structures.</returns>
		public static IEnumerable<ItemProperty> ToStruct(this IEnumerable<IItemProperty> properties)
		{
			if (properties == null)
				yield break;

			foreach (var prop in properties)
			{
				ItemProperty itemProp = new ItemProperty();
				itemProp.KeyName = prop.KeyName;
				itemProp.KeyValue = prop.KeyValue;
				yield return itemProp;
			}
			yield break;
		}


	}
}
